package com.suitsoft.bdms.web.system;

import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionSupport;
import com.suitsoft.bdms.entity.account.Authority;
import com.suitsoft.bdms.entity.system.STreeInfo;
import com.suitsoft.bdms.service.system.TreeInfoManager;
/**
 * 目录树管理类.
 * @author Genius
 *
 */
@SuppressWarnings("serial")
public class TreeInfoAction extends ActionSupport{
	/** 日志输出对象 */
	protected final Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	private TreeInfoManager treeInfoManager;
	
	/** 所有目录信息集 */
	private List<Map<String,Object>> treeInfos;
	
	/** 所有目录集. */
	private List<STreeInfo> treeDir;
	
	/** 目录树实体. */
	private STreeInfo streeInfo;
	
	/** 权限实体. */
	private Authority authority;
	
	private final String TARGET ="MAINFRAME";
	
	/** 标题与目录 区分 1 - 标题，2 - 目录. */
	private int isDir;
	
	/** 目录 */
	@SuppressWarnings("unused")
	private final int DIR = 2;
	/** 文件 */
	private final int NOTDIR = 1;
	
	private int id;
	

	@Override
	public String execute()  {
		treeInfos = treeInfoManager.findAllTreeInfo();
		treeDir = treeInfoManager.findAllDir();
		if(treeInfos == null || treeInfos.size() == 0){
			addActionMessage("没有目录树信息！");
		}
		return SUCCESS;
	}
	
	@Override
	public String input() {
		try {
			streeInfo.setTarget(TARGET);
			long newGourpId = treeInfoManager.getNewGoupId();
			streeInfo.setIcon(newGourpId+"");
			treeInfoManager.save(streeInfo);
			if(isDir ==NOTDIR){
				authority.setGroupId(newGourpId);
				treeInfoManager.save(authority);	
			}
			addActionMessage("添加成功！");
			execute();
		} catch (Exception e) {
			execute();
			addActionMessage("新增失败！");
		}
		return SUCCESS;
	}
	
	/**
	 * 删除前，判断当前，有没有子级.
	 * @return
	 */
	public void delBefore() throws Exception{
		treeDir = treeInfoManager.getSTreeInfosByParentId(id);
		HttpServletResponse response = ServletActionContext.getResponse();
		PrintWriter out =  response.getWriter();
		int f = 0;
		if(treeDir != null && treeDir.size() != 0){
			f = 1;
		} 
		out.print(f);
		out.flush();
		out.close();
	}
	
	/**
	 * 删除.
	 * @return
	 */
	public String del(){
		try {
			//不能删除ID为0-7的目录，因为，这是框架本身的.如果删除，框架的数据要重新初始化.
			if(id <=7){
				throw new Exception();
			}
			treeDir = treeInfoManager.getSTreeInfosByParentId(id);
			for(STreeInfo s : treeDir){
			List<Authority> la ;
	//		streeInfo = treeInfoManager.getEntiry(id);
			//判断当前是否为目录.
			if (s.getNodeurl() != null) {
				la = treeInfoManager.getAuthorityEntiry(Long.valueOf(s.getIcon()));
				if (la.size() != 1) {
					addActionMessage("权限表的数据有误，请手动去库检查！");
					addActionMessage("错误原因：权限组ID应该是唯一的！");
					return SUCCESS;
				}
				authority = la.get(0);
				treeInfoManager.deleteRole_Authority(authority.getId());
				treeInfoManager.delete(authority);
			}
			
			treeInfoManager.delete(s);
			}
			streeInfo = treeInfoManager.getEntiry(id);
			treeInfoManager.delete(streeInfo);
			treeInfoManager.flush();
			addActionMessage("删除成功!");
		} catch (Exception e) {
			addActionMessage("删除失败!");
		}
		execute();
		return SUCCESS;
	}
	
	/**
	 * 修改
	 * @return
	 */
	public String edit(){
		try {
			//不能删除ID为0-7的目录，因为，这是框架本身的.如果删除，框架的数据要重新初始化.
//			if(id <=11){
//				throw new Exception();
//			}
			List<Authority> la ;
			streeInfo = treeInfoManager.getEntiry(id);
			//nodurl 空时，说明当前是目录.
			if(streeInfo.getNodeurl() == null){
				isDir = 2;
			}else{
				isDir = 1;
				la = treeInfoManager.getAuthorityEntiry(Long.valueOf(streeInfo.getIcon()));
				if(la != null && la.size() != 1){
					addActionMessage("权限表的数据有误，请手动去库检修！");
					addActionMessage("错误原因：权限组ID应该是唯一的！");
					return SUCCESS;
				}
				authority = la.get(0);
			}
		} catch (Exception e) {
			addActionMessage("修改失败!");
			e.printStackTrace();
		}
		execute();
		return INPUT;
	}
	/**
	 * 修改提交.
	 * @return
	 */
	public String editSubmit(){
		try {
			STreeInfo sti = treeInfoManager.getEntiry(streeInfo.getNodeid());
			//目录 改  链接 
			if(sti.getNodeurl() == null && streeInfo.getNodeurl() != null){
				addActionMessage("目录不能改成链接！");
				execute();
				return INPUT;
				//链接 改 目录
			}else if(sti.getNodeurl() != null && streeInfo.getNodeurl() == null){
				addActionMessage("链接不能改链接！");
				execute();
				return INPUT;
			}
			streeInfo.setTarget(TARGET);
			treeInfoManager.update(streeInfo);
			//如果不是目录 就更新资源表.
			if(isDir == 1){
				treeInfoManager.update(authority);
			}
			addActionMessage("修改成功！");
		} catch (Exception e) {
			addActionMessage("修改失败");
			e.printStackTrace();
		}
		execute();
		return SUCCESS;
	}

	public List<Map<String, Object>> getTreeInfos() {
		return treeInfos;
	}

	public void setTreeInfos(List<Map<String, Object>> treeInfos) {
		this.treeInfos = treeInfos;
	}

	public List<STreeInfo> getTreeDir() {
		return treeDir;
	}

	public void setTreeDir(List<STreeInfo> treeDir) {
		this.treeDir = treeDir;
	}

	public STreeInfo getStreeInfo() {
		return streeInfo;
	}

	public void setStreeInfo(STreeInfo streeInfo) {
		this.streeInfo = streeInfo;
	}

	public Authority getAuthority() {
		return authority;
	}

	public void setAuthority(Authority authority) {
		this.authority = authority;
	}

	public int getIsDir() {
		return isDir;
	}

	public void setIsDir(int isDir) {
		this.isDir = isDir;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}
}
